package com.jason.kotlin.gank.ui

import android.graphics.Bitmap
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.chad.library.adapter.base.BaseBinderAdapter
import com.gyf.immersionbar.ImmersionBar
import com.jason.kotlin.gank.GankConstant
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.ui.adapter.binder.WeatherCalendarItemBinder
import com.jason.kotlin.gank.ui.adapter.binder.WeatherHeaderItemBinder
import com.jason.kotlin.gank.ui.adapter.binder.WeatherLaterItemBinder
import com.jason.kotlin.gank.ui.adapter.binder.WeatherSuggestionItemBinder
import com.jason.kotlin.gank.viewmodel.WeatherViewModel
import com.jason.library.common.base.BaseStateActivity
import com.jason.library.common.ext.observe
import com.jason.library.common.util.CLogUtils
import com.jason.library.common.util.DateUtils
import jp.wasabeef.blurry.Blurry

/**
 * @description: WeatherActivity
 *
 * @author ChenBaoyang
 * @date 4/27/21 11:30
 */
class WeatherActivity : BaseStateActivity<WeatherViewModel>() {

    private lateinit var bgIv: ImageView
    private lateinit var bgIv2: ImageView
    private lateinit var bgBlurLl: LinearLayout

    private lateinit var backRl: RelativeLayout
    private lateinit var titleTv: TextView
    private lateinit var contentLl: LinearLayout

    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView

    private lateinit var provinceName: String
    private lateinit var cityName: String
    private lateinit var location: String

    private val defaultAlpha = 0.0f
    private val maxAlpha = 1.0f

    private val adapter = BaseBinderAdapter()

    override fun bindLiveData() {
        observe(viewModel!!.data) {
            adapter.setList(it)
            refreshLayout.isRefreshing = false
        }

    }

    override fun parseIntent() {
        provinceName = intent.getStringExtra(GankConstant.KEY_PROVINCE_NAME)?: "福建省"
        cityName = intent.getStringExtra(GankConstant.KEY_CITY_NAME) ?: "厦门市"
        location = intent.getStringExtra(GankConstant.KEY_LOCATION).toString()
        CLogUtils.i("------$location")
    }

    override fun getPageTarget() = refreshLayout

    override fun initView() {
        bgIv = findViewById(R.id.iv_bg)
        bgIv2 = findViewById(R.id.iv_bg2)
        bgBlurLl = findViewById(R.id.ll_bg_blur)

        backRl = findViewById(R.id.rl_back)
        titleTv = findViewById(R.id.tv_title)
        titleTv.text = "$provinceName-$cityName"
        contentLl = findViewById(R.id.ll_content)

        refreshLayout = findViewById(R.id.swipeRefreshLayout)
        recyclerView = findViewById(R.id.recyclerView)

        initItemBinder()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            private var totalDy = 0
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val height = recyclerView.height
                totalDy -= dy
                var alpha: Float =
                    Math.abs(totalDy) * maxAlpha / 700
                if (alpha > maxAlpha) {
                    alpha = maxAlpha
                }
                if (alpha < defaultAlpha) {
                    alpha = defaultAlpha
                }
                CLogUtils.i(
                    "dy:$dy,totalDy:$totalDy,height:$height,alpha:$alpha"
                )
                bgBlurLl.alpha = alpha
            }
        })

        backRl.setOnClickListener {
            finish()
        }

        refreshLayout.setOnRefreshListener {
            onDataInit()
        }

        initBackground()

        ImmersionBar.with(this).titleBar(contentLl).transparentStatusBar().init()
    }

    private fun initBackground() {
        val options = RequestOptions()
        options.centerCrop()
        Glide.with(this@WeatherActivity)
            .asBitmap()
            .load(GankConstant.DEFAULT_IMAGE_URL)
            .apply(options)
            .into(object : SimpleTarget<Bitmap?>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap?>?
                ) {
                    bgIv.setImageBitmap(resource)
                    bgIv2.setImageBitmap(resource)
                    Blurry.with(this@WeatherActivity)
                        .radius(25)
                        .sampling(3)
                        .async()
                        .capture(bgIv2)
                        .into(bgIv2)
                    bgBlurLl.alpha = defaultAlpha
                }
            })
    }

    private fun initItemBinder() {
        adapter.addItemBinder(WeatherHeaderItemBinder())
            .addItemBinder(WeatherLaterItemBinder())
            .addItemBinder(WeatherSuggestionItemBinder())
            .addItemBinder(WeatherCalendarItemBinder())
    }

    override fun reload() {
        onDataInit()
    }

    override fun getLayoutId() = R.layout.activity_weather

    override fun onDataInit() {
        viewModel!!.loadWeather(DateUtils.getToday(), location, cityName)
    }
}